<?php 
defined('RUN_KATANA') or die('Restricted access');
class ProductVariation extends Object
{
	public $ProductId;
	public $Varnum;
	public $VarTitle;
	public $Identity;
	public $ExtIdentity;
	public $BuyMin;
	public $StockCount;
	public $StockAddDate;
	public $StockAddQuan;
	public $StockAddTxt;
	public $NetWeight;
	public $GrosWeight;
	public $Length;
	public $Width;
	public $Height;
	public $StdPrice;
	public $NowPrice;
	public $NowPriceDateEnd;
	public $OfferPrice;
	public $OfferStart;
	public $OfferEnd;
	public $Active;
	
	public $Attributes; //2 dim array with titles and values and [id]
	
	public $TaxId;
	public $TaxValue;
	
	public function ProductVariation() 
	{ 
		self::setDataChanged(false);
		self::setTaxId(0);
		self::setTaxValue(0);
	}
	
	/* GET FUNCTIONS */
	public function getProductId() { return intval($this->ProductId); }
	public function getVarnum() { return intval($this->Varnum); }
	public function getBuyMin() { return intval($this->BuyMin); }
	public function getStockCount() { return intval($this->StockCount); }
	public function getStockAddDate() { return $this->StockAddDate; }
	public function getStockAddQuan() { return intval($this->StockAddQuan); }
	public function getNetWeight() { return intval($this->NetWeight); }
	public function getGrosWeight() { return intval($this->GrosWeight); }
	public function getLength() { return intval($this->Length); }
	public function getWidth() { return intval($this->Width); }
	public function getHeight() { return intval($this->Height); }
	public function getStdPrice($tax = false, $round = 0, $format=true) 
	{ 
		global $shop;
		if(is_numeric($this->StdPrice))
		{
			if(($tax == false) || (self::getTaxId() == 0))
			{ 
				if($round > 0)
				{ 
					if($format)
					{ return number_format(round($this->StdPrice,$round),$shop->getSetting('MoneyFormatDecimals'),$shop->getSetting('MoneyFormatDecPoint'),$shop->getSetting('MoneyFormatThousSep')); }
					else
					{ return round($this->StdPrice, $round); }
				}
				else
				{ return $this->StdPrice; }
			}
			else
			{ 
				if($format)
				{ return number_format(round(($this->StdPrice * self::getTaxValue()),1),$shop->getSetting('MoneyFormatDecimals'),$shop->getSetting('MoneyFormatDecPoint'),$shop->getSetting('MoneyFormatThousSep')); }
				else
				{ return round(($this->StdPrice * self::getTaxValue()),1); }
			}
		}
		else
		{ 
			$this->StdPrice = 0;
			return $this->StdPrice;
		}
	}
	public function getNowPrice($tax = true, $round = 0, $format=true) 
	{ 
		global $shop;
		if(is_numeric($this->NowPrice))
		{
			if($tax)
			{
				if($format)
				{ return number_format(round(($this->NowPrice),1),$shop->getSetting('MoneyFormatDecimals'),$shop->getSetting('MoneyFormatDecPoint'),$shop->getSetting('MoneyFormatThousSep')); }
				else
				{ return round(($this->NowPrice),1); }
			}
			else
			{
				if($shop->getSetting('NowPriceIncTax'))
				{
					if($format)
					{ return number_format(round(($this->NowPrice / self::getTaxValue()),1),$shop->getSetting('MoneyFormatDecimals'),$shop->getSetting('MoneyFormatDecPoint'),$shop->getSetting('MoneyFormatThousSep')); }
					else
					{ return round(($this->NowPrice / self::getTaxValue()),1); }
				}
				else
				{
					if($format)
					{ return number_format(round(($this->NowPrice),1),$shop->getSetting('MoneyFormatDecimals'),$shop->getSetting('MoneyFormatDecPoint'),$shop->getSetting('MoneyFormatThousSep')); }
					else
					{ return round(($this->NowPrice),1); }
				}
			}
		}
		else
		{ 
			$this->NowPrice = 0;
			return $this->NowPrice;
		}
	}
	public function getNowPriceDateEnd($format = '') 
	{ 
		if($format != '')
		{ return self::formatDateTime($this->NowPriceDateEnd, $format); }
		else
		{ return $this->NowPriceDateEnd; }
	}
	
	public function getOfferPrice() { return $this->OfferPrice; }
	public function getOfferStart($format = '') 
	{ 
		if($format != '')
		{ return self::formatDateTime($this->OfferStart, $format); }
		else
		{ return $this->OfferStart; }
	}
	public function getOfferEnd($format = '') 
	{ 
		if($format != '')
		{ return self::formatDateTime($this->OfferEnd, $format); }
		else
		{ return $this->OfferEnd; }
	}
	
	public function getAttributeValue($attId = 0) 
	{ return $this->Attributes; }
	public function getAttributeValueId($attID=0) 
	{ 
		if($attID > 0)
		{
			if(isset($this->Attributes[$attID]))
			{
				if(isset($this->Attributes[$attID]['value']['value_id']))
				{ return $this->Attributes[$attID]['value']['value_id']; }
			}
		}
	}
	public function getAttributes() { return $this->Attributes; }
	
	public function getTaxId() { return $this->TaxId; }
	public function getTaxValue() { return $this->TaxValue; }
	
	/* SET FUNCTIONS */
	public function setProductId($val) { $this->ProductId=intval($val); }
	public function setVarnum($val) { $this->Varnum=intval($val); }
	public function setBuyMin($val) { $this->BuyMin=intval($val); }
	public function setStockCount($val) { $this->StockCount=intval($val); }
	public function setStockAddDate($val) { $this->StockAddDate=$val; }
	public function setStockAddQuan($val) { $this->StockAddQuan=intval($val); }
	public function setNetWeight($val) { $this->NetWeight=intval($val); }
	public function setGrosWeight($val) { $this->GrosWeight=intval($val); }
	public function setLength($val) { $this->Length=intval($val); }
	public function setWidth($val) { $this->Width=intval($val); }
	public function setHeight($val) { $this->Height=intval($val); }
	public function setStdPrice($val) { $this->StdPrice=$val; }
	public function setNowPrice($val) { $this->NowPrice=$val; }
	public function setOfferPrice($val) { $this->OfferPrice=$val; }
	public function setOfferStart($val) { $this->OfferStart=$val; }
	public function setOfferEnd($val) { $this->OfferEnd=$val; }
	
	public function setAttributes($val) { $this->Attributes=$val; }	
	public function setAttributeValueId($attributes, $attID=0, $valID=0) 
	{ 
		if((isset($this->Attributes[$attID])) && (isset($attributes[$attID]['predefined_values'][$valID])))
		{
			$this->Attributes[$attID]['value']['value_id'] = $valID;
			$this->Attributes[$attID]['value']['product_id'] = self::getProductId();
			$this->Attributes[$attID]['value']['variation_id'] = self::getId();
			$this->Attributes[$attID]['value']['attribute_id'] = $attID;
			$this->Attributes[$attID]['value']['value'] = $attributes[$attID]['predefined_values'][$valID]['value'];
			
			if(isset($this->Attributes[$attID]['value']['id']) && (intval($this->Attributes[$attID]['value']['id']) > 0))
			{
				$sql = 'UPDATE '.DB_PERFIX.'product_var_vals SET ';
				$sql .= '`value_id`='.$valID.', ';
				$sql .= '`value`=\''.mysql_real_escape_string($this->Attributes[$attID]['value']['value']).'\' ';
				$sql .= 'WHERE `id`='.intval($this->Attributes[$attID]['value']['id']).';';
				$result = mysql_query($sql);
				unset($result);
				unset($sql);
				//echo $sql.'<br />';
			}
			else
			{
				$sql = 'DELETE FROM '.DB_PERFIX.'product_var_vals WHERE variation_id='.self::getId().' AND product_id='.self::getProductId().' AND attribute_id='.intval($attID).';';
				mysql_query($sql);
				unset($sql);
				//echo $sql.'<br />';
				
				$sql =  'INSERT INTO '.DB_PERFIX.'product_var_vals (';
				$sql .= '`product_id`, ';
				$sql .= '`variation_id`, ';
				$sql .= '`attribute_id`, ';
				$sql .= '`value_id`, ';
				$sql .= '`value` ';
				$sql .= ') ';
				$sql .= ' VALUES (';
				$sql .= self::getProductId().', ';
				$sql .= self::getId().', ';
				$sql .= intval($attID).', ';
				$sql .= intval($valID).', ';
				$sql .= '\''.mysql_real_escape_string($this->Attributes[$attID]['value']['value']).'\' ';
				$sql .= ');';
				mysql_query($sql);
				//echo $sql.'<br />';
				unset($sql);
			}
		}
	}	
	
	public function setTaxId($val) { $this->TaxId=$val; }
	public function setTaxValue($val) { $this->TaxValue=$val; }
	
	public function update()
	{
		if(self::getId() > 0)
		{
			global $shop;
			
			$sql  = 'UPDATE '.DB_PERFIX.'product_vars SET ';
			$sql .= '`varnum`='.$this->Varnum.', ';
			$sql .= '`var_title`=\''.mysql_real_escape_string($this->VarTitle).'\', ';
			$sql .= '`identity`=\''.mysql_real_escape_string($this->Identity).'\', ';
			$sql .= '`ext_identity`=\''.mysql_real_escape_string($this->ExtIdentity).'\', ';
			if($shop->GlobalSettings['buy_min'])
			{ $sql .= '`buy_min`='.$this->BuyMin.', '; }
			if($shop->GlobalSettings['stock'])
			{
				$sql .= '`stock_count`='.$this->StockCount.', ';
				//$sql .= '`stock_add_date`=\''.mysql_real_escape_string($this->StockAddDate).'\', ';
				$sql .= '`stock_add_quan`='.$this->StockAddQuan.', ';
				$sql .= '`stock_add_txt`=\''.mysql_real_escape_string($this->StockAddTxt).'\', ';
			}
			if($shop->GlobalSettings['shipping_weight_volume_active'])
			{
				$sql .= '`net_weight`='.self::getNetWeight().', ';
				$sql .= '`gros_weight`='.self::getGrosWeight().', ';
			}
			$sql .= '`length`='.self::getLength().', ';
			$sql .= '`width`='.self::getWidth().', ';
			$sql .= '`height`='.self::getHeight().', ';
			
			$sql .= '`std_price`='.$this->StdPrice.', ';
			if($shop->GlobalSettings['now_price'])
			{ 
				$sql .= '`now_price`='.$this->NowPrice.', ';
				$sql .= '`NowPriceDateEnd`="'.self::getNowPriceDateEnd('Y-m-d H:i:s').'", ';
			}
			if($shop->GlobalSettings['offer_price'])
			{
				$sql .= '`offer_price`='.$this->OfferPrice.', ';
				$sql .= '`offer_start`="'.self::getOfferStart().'", ';
				$sql .= '`offer_end`="'.self::getOfferEnd().'", ';
			}
			$sql .= '`active`='.self::getActive().', ';
			$sql .= '`product_id`='.$this->ProductId.' ';
			$sql .= 'WHERE `id`='.$this->Id.';';
			
			echo '<br>'.$sql;
			$result = mysql_query($sql);
		}
	}
}
?>